package cn.com.io.day20151214.control;

import cn.com.io.day20151214.pojo.Person;
import cn.com.io.day20151214.pojo.Teacher;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * 对象引用序列化(一)
 */
public class ReadTeacher {
    public static void main(String[] args) throws IOException {
        ObjectInputStream ois = null ;
        try {
            ois = new ObjectInputStream(new FileInputStream("teacher.txt"));//创建ObjectInputStream输入流
            //依次读取ObjectInputStream输入流中的四个对象
            Teacher teacher1 = (Teacher)ois.readObject();
            Teacher teacher2 = (Teacher)ois.readObject();
            Person person = (Person)ois.readObject();
            Teacher teacher3 = (Teacher)ois.readObject();
            System.out.println("teacher1的student引用和person是否相同："+(teacher1.getStudent() == person));
            System.out.println("teacher2的student引用和person是否相同："+(teacher2.getStudent() == person));
            System.out.println("teacher2和teacher3是否是同一个对象："+(teacher2 == teacher3));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(ois != null){
                ois.close();
            }
        }
    }
}
